package com.lu.model.entity;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 系统审批配置
 * </p>
 *
 * @author currentCodeAuthor
 * @since 2022-08-01
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class SysApprovalConfig extends Model<SysApprovalConfig> {

    private static final long serialVersionUID = 1L;

    public static final Long END_NODE_ID = -1L;

    public static final Map<String, SysApprovalConfig> CONFIG_MAP = new ConcurrentHashMap<>();

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 下一个节点的id
     */
    private Long nextId;

    /**
     * 商户审批
     */
    private String title;

    /**
     * 场景表名
     */
    private String tableName;

    /**
     * 需要更新table_name表的target_field字段的值
     */
    private String targetField;

    /**
     * 目标实体名称字段
     */
    private String targetNameField;

    /**
     * 未审核标识
     */
    private Integer unAuditedSign;

    /**
     * 等待标志
     */
    private Integer waitSign;

    /**
     * 未通过标志(审批未通过，修改目标的状态)
     */
    private Integer noPassSign;

    /**
     * 通过标志(审批通过，修改目标的状态)
     */
    private Integer passSign;

    /**
     * table_name表的target_field字段的值 为 禁用
     */
    private Integer disableSign;

    /**
     * 审核通过后 需要更新table_name表的target_field字段的值 为 启用
     */
    private Integer enableSign;

    /**
     * 当前节点可以审批的角色 逗号分隔
     */
    private String approvedRoleIds;

    /**
     * 当前节点可以审批的角色 逗号分隔
     */
    private String approvedUserIds;

    private String updateUserIdField;

    private String updateTimeField;

    /**
     * mapper bean 全路径
     */
    private String mapperBean;

    @Override
    protected Serializable pkVal() {
        return this.id;
    }

    /**
     * 下一个节点
     */
    @TableField(exist = false)
    private SysApprovalConfig nextConfig;

    public static void build(List<SysApprovalConfig> configList){
        if(ObjectUtil.isEmpty(configList)){
            return;
        }
        for (SysApprovalConfig approvalConfig : configList) {
            SysApprovalConfig config = CONFIG_MAP.get(approvalConfig.getTableName());
            SysApprovalConfig item = new SysApprovalConfig();
            BeanUtil.copyProperties(approvalConfig, item);
            if(config == null){
                config = item;
                CONFIG_MAP.put(approvalConfig.getTableName(), config);
                continue;
            } else {
                buildNext(config, item);
                CONFIG_MAP.put(approvalConfig.getTableName(), config);
            }
        }
    }

    public static void buildNext(SysApprovalConfig config, SysApprovalConfig item){
        if(config.getNextConfig() == null){
            config.setNextConfig(item);
        }else {
            buildNext(config.getNextConfig(), item);
        }
    }

    public static SysApprovalConfig get(SysApprovalConfig config, Long id){
        if(config.getNextId() == END_NODE_ID){
            return config;
        }
        if(config.getId() == id){
            return config;
        }
        return get(config.getNextConfig(), id);
    }

}
